﻿using Bll;
using Dal.DTO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;

namespace WebApi0207.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ShopController : ControllerBase
    {
        public ShopController(ShopBll bll,IWebHostEnvironment web)
        {
            Bll = bll;
            Web = web;
        }

        public ShopBll Bll { get; }
        public IWebHostEnvironment Web { get; }

        /// <summary>
        /// 添加门店
        /// </summary>
        /// <param name="shop">一个门店对象</param>
        /// <returns>受影响行数</returns>
        [HttpPost]
        public int Add(Shop shop)
        {
            try
            {
                return Bll.Add(shop);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改门店
        /// </summary>
        /// <param name="shop">一个门店对象</param>
        /// <returns>受影响行数</returns>
        [HttpPost]
        public int Update(Shop shop)
        {
            try
            {
                return Bll.Update(shop);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 逻辑删除门店
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns>受影响行数</returns>
        [HttpDelete]
        public int Delete(int id)
        {
            try
            {
                return Bll.Delete(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 分页显示，门店名称模糊查，门店地址精确查
        /// </summary>
        /// <param name="index">当前页</param>
        /// <param name="size">页容量</param>
        /// <param name="name">门店名称</param>
        /// <param name="addr">门店地址</param>
        /// <returns>DTO</returns>
        [HttpGet]
        public ShopDTO GetAll(int index, int size, string? name, string? addr)
        {
            try
            {
                return Bll.GetAll(index, size, name, addr);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 通过id获取一个门店对象
        /// </summary>
        /// <param name="id">门店id</param>
        /// <returns>一个门店对象</returns>
        [HttpGet]
        public Shop GetShop(int id)
        {
            try
            {
                return Bll.GetShop(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 查所有地址
        /// </summary>
        /// <returns>地址集合</returns>
        [HttpGet]
        public List<Addr> GetAddrs()
        {
            return Bll.GetAddrs();
        }
        [HttpPost]
        public string UpFile(IFormFile file)
        {
            var id=Guid.NewGuid().ToString();
            var filename=Web.WebRootPath+ @"\pic\" + id+file.FileName;
            using(FileStream fs=System.IO.File.Create(filename))
            {
                file.CopyTo(fs);
                fs.Close();
            }
            return "/pic/"+id+file.FileName;
        }
    }
}
